﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using WeFeTeLib;

namespace WeFeTeConsole
{
    class Program
    {
        static int totalProgress = 0;
        static void Main(string[] args)
        {
            FrameworkTester tester;

            Console.WriteLine("WeFeTe Framework Tester");

            if (args.Length < 1 || args.Length > 2)
            {
                Console.WriteLine("Please, insert the parameters in the following way:\n" +
                        "WeFeTeConsole.exe <URL> <Framework>\n" +
                        "Use WeFeTeConsole.exe show to display the list of available frameworks.");
                return;
            }

            if (args[0].ToUpper() == "SHOW")
            {
                Console.WriteLine("These are the current available frameworks:");
                foreach (FrameworkFactory.FrameworkType framework in Enum.GetValues(typeof(FrameworkFactory.FrameworkType)))
                {
                    Console.WriteLine("- " + framework.ToString());
                }
                return;
            }
            
            if(args.Length == 1)
            {
                Console.WriteLine("Please, insert a Framework to test or all for all frameworks");
                return;
            }

            if (args[1].ToUpper() == "ALL")
            {
                foreach (FrameworkFactory.FrameworkType framework in Enum.GetValues(typeof(FrameworkFactory.FrameworkType)))
                {
                    tester = new FrameworkTester(args[0], framework);
                    tester.OnPartCompleted += new FrameworkTester.OnPartCompletedDelegated(tester_OnPartCompleted);
                    tester.OnTestFinished += new FrameworkTester.OnTestFinishedDelegated(tester_OnTestFinished);

                    totalProgress = 0;
                    Console.Write("\r\n0% ");
                    tester.Start();
                }
                return;
            }

            FrameworkFactory.FrameworkType selectedFramework = new FrameworkFactory.FrameworkType();
            foreach (FrameworkFactory.FrameworkType framework in Enum.GetValues(typeof(FrameworkFactory.FrameworkType)))
            {
                if (args[1].ToUpper() == framework.ToString().ToUpper())
                {
                    selectedFramework = framework;
                }
            }

            tester = new FrameworkTester(args[0], selectedFramework);
            tester.OnPartCompleted += new FrameworkTester.OnPartCompletedDelegated(tester_OnPartCompleted);
            tester.OnTestFinished += new FrameworkTester.OnTestFinishedDelegated(tester_OnTestFinished);

            totalProgress = 0;
            Console.Write("\r\n0%");
            tester.Start();
        }

        static void tester_OnTestFinished(string name, int percent, List<Result> framework)
        {
            Console.WriteLine("\r\nFramework: " + name);
            Console.WriteLine("Accuracy: " + percent);
            foreach (Result r in framework)
            {
                if (r.Conditions >= 1)
                {
                    Console.Write("\t- ");
                }
                else
                {
                    Console.Write("\tx ");
                }
                Console.Write("Type:" + r.ResourceType);
                Console.WriteLine("\tName: " + r.ResourceName);
            }
        }

        static void tester_OnPartCompleted(int amount)
        {
            totalProgress += amount;
            Console.Write(" ---------- " + totalProgress + "%");
        }
    }
}
